#include "ConfigFile.h"
#include "gui/MainFrame.h"
#include "Configuration/Robot.h"
#include <cv.h>
#include <math.h>
#include <locale.h>

ConfigFile::ConfigFile(char* fileName, char* filePath, bool read)
{
	setlocale(LC_ALL, "C");

	char* temp = new char[255];
	
	sprintf(temp,"%s%s",filePath,fileName);
	
	if(read)
		file = fopen(temp,"r");
	else
		file = fopen(temp,"w+");
		
	if(file)
	{
		this->fileName = fileName;
		this->filePath = filePath;
	}
	
	delete temp;
	
}

ConfigFile::~ConfigFile()
{
	close();
	fileName = NULL;
	filePath = NULL;
}

bool ConfigFile::readConfigFile()
{
	readColorId();
		
}

bool ConfigFile::readColorId()
{
	char* temp = new char[100];
	if(fscanf(file,"%s",temp)==EOF)
		return false;
	if (temp==INIT_COLOR)
		printf("si paso\n");
	
	
}


bool ConfigFile::writeDriverCamera(int cameraIndex, int width, int height)
{
	writeString((char*)INIT_DRIVER);
	fprintf(file,"\tname \"StreamCamera\"\n");
	fprintf(file,"\tplugin \"libstreamcamera\"\n");
	fprintf(file,"\tprovides [\"camera:0\"]\n");
	fprintf(file,"\tsize [%d %d]\n",width,height);
	fprintf(file,"\tipaddr \"localhost\"\n");
	fprintf(file,"\tport 8080\n");
	fprintf(file,"\taction \"snapshot\"\n");
	fprintf(file,"\tfile_url \"./pruebas.jpg\"\n");
	fprintf(file,"\tcurl_debug 0\n");
	fprintf(file,")\n\n");
	flush();
}

bool ConfigFile::writeDriverBlobfinder(char* colorFile)
{
	writeString((char*)INIT_DRIVER);
	fprintf(file,"\tname \"cmvision\"\n");
	fprintf(file,"\tprovides [\"blobfinder:0\"]\n");
	fprintf(file,"\trequires [\"camera:0\"]\n");
	
	fprintf(file,"\tcolorfile \"%s\"\n",colorFile);
	
	fprintf(file,")\n\n");
	flush();
	
}
bool ConfigFile::writePoints(CvPoint* imagePoints, CvPoint2D32f* realPoints)
{
	//fprintf(file,"matrix-points\n");
	fprintf(file,"imagePoints: ");

	for(int i=0;i<4;i++)
	{
		fprintf(file,"(%d,%d)\t",imagePoints[i].x,imagePoints[i].y);
	}

	fprintf(file,"\nrealPoints: ");
	for(int i=0;i<4;i++)
	{
		fprintf(file,"(%4.2f,%4.2f)\t",realPoints[i].x,realPoints[i].y);
	}
	flush();
}


bool ConfigFile::writeDriverRobotPos(Robot* robot,int index, double* matrix, float* distParams, float* intrinsicParams)
{
	
	//TODO: falta cambiar para que se pueda definir el puerto de comienzo
	int initPort = 6665;
	writeString((char*)INIT_DRIVER);
	fprintf(file,"\tname \"BFPosition\"\n");
	fprintf(file,"\tplugin \"libbfposition\"\n");
	
	fprintf(file,"\tprovides [\"%d:position2d:0\"]\n",initPort+index);
	fprintf(file,"\trequires [\"blobfinder:0\"]\n");
	
	//base and robot colors
	fprintf(file,"\tbaseColor %d\n",robot->getBaseColor()->getMeanValueInt(RGB_CODE));
	fprintf(file,"\trobotColor %d\n",robot->getRobotColor()->getMeanValueInt(RGB_CODE));
	
	//min dist and max dist
	fprintf(file,"\tminArea %d\n",robot->getMinArea());
	fprintf(file,"\tmaxArea %d\n",robot->getMaxArea());
	
	//min dist and max dist
	fprintf(file,"\tminDist %d\n",robot->getMinDist());
	fprintf(file,"\tmaxDist %d\n",robot->getMaxDist()); 
	
	if(matrix!=NULL)
	{
		fprintf(file,"\tmatrix [ ");	
		for(int i=0;i<9;i++)
		{
			fprintf(file,"%f ",matrix[i]);
		}
		fprintf(file,"]\n");	
		
	
	}
	
	if(intrinsicParams!=NULL)
	{
		fprintf(file,"\tintrinsic [ ");	
		for(int i=0;i<9;i++)
		{
			fprintf(file,"%f ",intrinsicParams[i]);
		}
		fprintf(file,"]\n");	
		
	
	}
	
	if(distParams!=NULL)
	{
		fprintf(file,"\tradial [ ");	
		for(int i=0;i<2;i++)
		{
			fprintf(file,"%f ",distParams[i]);
		}
		fprintf(file,"]\n");	
		
	
	}
	
	fprintf(file,")\n\n");
	flush();
	
}

bool ConfigFile::writeObstConfig(nxColor* color,double* matrix, float* distParams, float* intrinsicParams,int minArea, int maxArea)
{
	//base and robot colors
	fprintf(file,"obstColor %d %s\n",color->getMeanValueInt(RGB_CODE),color->getName());
	//min dist and max dist
	fprintf(file,"minArea %d\n",minArea);
	fprintf(file,"maxArea %d\n",maxArea);
	
	if(matrix!=NULL)
	{
		fprintf(file,"matrix ");	
		for(int i=0;i<9;i++)
		{
			fprintf(file,"%f ",matrix[i]);
		}
		fprintf(file,"\n");	
		
	
	}
	
	if(intrinsicParams!=NULL)
	{
		fprintf(file,"intrinsic ");	
		for(int i=0;i<9;i++)
		{
			fprintf(file,"%f ",intrinsicParams[i]);
		}
		fprintf(file,"\n");	
		
	
	}
	
	if(distParams!=NULL)
	{
		fprintf(file,"radial ");	
		for(int i=0;i<2;i++)
		{
			fprintf(file,"%f ",distParams[i]);
		}
		fprintf(file,"\n");	
		
	
	}
	flush();
}

bool ConfigFile::writeColors(nxColor** colorList, int colorListIndex)
{

	
	
	//Write color parameters
	nxColor* color;
	
	for(int i=0;i<colorListIndex;i++)
	{

		writeString((char*)INIT_COLOR);
		writeCRLF();
		//get color
		color = colorList[i];
		
		CvScalar meanRGB = color->getMeanValue(RGB_CODE);
		
		CvScalar meanHLS = color->getMeanValue(HLS_CODE);
		
		CvScalar meanHSV = color->getMeanValue(HSV_CODE);
		
		char* colorName = new char[30];
		
		colorName = color->getName();
	
		fprintf(file,"meanvalRGB [%f %f %f]\n",meanRGB.val[0],meanRGB.val[1],meanRGB.val[2]);
		fprintf(file,"meanvalHLS [%f %f %f]\n",meanHLS.val[0],meanHLS.val[1],meanHLS.val[2]);
		fprintf(file,"meanvalHSV [%f %f %f]\n",meanHSV.val[0],meanHSV.val[1],meanHSV.val[2]);
		
		
		fprintf(file,"name %s\n",colorName);
		writeString((char*)END_PARAM);
		writeCRLF();
	
		
	}
	
		
	flush();
	return true;
}
/*
bool FileConfig::readColors(vcColor** colorList,int* colorListIndex)
{
	float val0;
	float val1;
	float val2;
	
	
	fscanf(file,"meanvalRGB [%f %f %f]\n",val0,val1,val2);
	cout<<val0<<val1<<val2<<endl;

}*/
bool ConfigFile::writeRobots(RobotProject* rp)
{
	//get array of robots and size
	Robot** array = rp->getRobotArray();
	int numRobots = rp->getNumRobots();
	
	//header of section
	writeString((char*)INIT_ROBOT);
	writeCRLF();
	
	for(int i=0;i<numRobots;i++)
	{
		Robot* robot = array[i];
		
		//TODO: falta guardar valores de minDist y maxDist
		char* name = robot->getName();
		
		//write name of robot
		writeString(name);
		writeTAB();
		
		int baseColor = robot->getBaseColor()->getMeanValueInt(RGB_CODE);
		int robotColor = robot->getRobotColor()->getMeanValueInt(RGB_CODE);
		
		writeInt(baseColor);
		writeTAB();
		writeInt(robotColor);
		writeCRLF();
		
		
	}
	writeString((char*)END_PARAM);
	writeCRLF();
	flush();
}

bool ConfigFile::writeAreas(int minArea, int maxArea)
{
	writeString((char*)INIT_AREAS);
	writeCRLF();
	writeInt(minArea);
	writeCRLF();
	writeInt(maxArea);
	writeCRLF();
	writeString(END_PARAM);
	writeCRLF();
	flush();
	return true;
	
	
}
	
bool ConfigFile::writeString(char* string)
{
	if(fprintf(file,"%s",string)>0)
		return true;
	
	return false;
}

bool ConfigFile::writeDouble(double value)
{
	if(fprintf(file,"%0.0f",value)>0)
		return true;
	
	return false;
	}

bool ConfigFile::writeInt(int value)
{
	if(fprintf(file,"%d",value)>0)
		return true;
	
	return false;
}

bool ConfigFile::readDouble(double *value)
{
	if(fscanf(file,"%f",value)==EOF)
		return false;
	else
	return true;
}

bool ConfigFile::readInt(int* value)
{
	if(fscanf(file,"%d",value)==EOF)
			return false;
		else
		return true;
}

bool ConfigFile::readString(char* string)
{
	
	if(fscanf(file,"%s\n",string)==EOF)
		return false;
	else
	return true;
}

bool ConfigFile::flush()
{
	if(!fflush(file))
		return true;
	return false;
}

bool ConfigFile::close()
{
	if(!fclose(file))
		return true;
	return false;
}

bool ConfigFile::writeCRLF()
{
	return writeString((char*)"\r\n");
}

bool ConfigFile::writeTAB()
{
	return writeString((char*)"\t");
}
